package com.netease.web.site;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.netease.domain.Commodity;
import com.netease.domain.CommodityModel;
import com.netease.domain.PageInfo;
import com.netease.domain.User;
import com.netease.domain.User_Commodity;
import com.netease.service.CommodityService;

@Controller
public class IndexController {
	@Autowired
	private CommodityService commodityService;

	/**
	 * Index
	 * @param model
	 * @param request
	 */
	@RequestMapping(value={"","/","index"})
	public String index(Model model, HttpServletRequest request) {
		String page = request.getParameter("page");
		page = StringUtils.defaultIfBlank(page, "1");
		int pageNumber = Integer.valueOf(page);
		
		int total = commodityService.findCount();
		PageInfo<Commodity> pageInfo = new PageInfo<>(total, pageNumber);
		pageInfo.setList(commodityService.findAllByPage(pageInfo.getPageStart()));
		
		model.addAttribute("page", pageInfo);
		return "index";
	}
	
	@RequestMapping("/detail")
	public String detail(Model model, HttpServletRequest request) {
		String id = request.getParameter("id");
		id = StringUtils.defaultIfBlank(id, "1");

		

		Commodity c = commodityService.findOne(id);
		
		
		model.addAttribute("commodity", c);
		return "detail";
	}
	
}